package cn.sirun.com.fca.account.view.impl;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.jungly.gridpasswordview.GridPasswordView;
import com.tencent.cloud.iov.common.flow.view.impl.BaseLoadingFlowView;
import com.tencent.cloud.iov.flow.view.IContentViewContainer;
import com.tencent.cloud.iov.kernel.constant.StatusCode;
import com.tencent.cloud.iov.kernel.model.BaseResponse;
import com.tencent.cloud.iov.router.Router;
import com.tencent.cloud.iov.util.ActivityUtils;
import com.tencent.cloud.iov.util.SRPUtils;
import com.tencent.cloud.iov.util.crypt.CustomSRPClient;
import com.tencent.cloud.uikit.utils.DialogUtils;

import cn.sirun.com.fca.account.R;
import cn.sirun.com.fca.account.constant.AccountActivityConst;
import cn.sirun.com.fca.account.constant.AccountIntentConst;
import cn.sirun.com.fca.account.constant.ControlIntentConst;
import cn.sirun.com.fca.account.model.bind.PinSetParamResponse;
import cn.sirun.com.fca.account.model.bind.PinSetParamsRequest;
import cn.sirun.com.fca.account.model.bind.VerifyPinRequest;
import cn.sirun.com.fca.account.presenter.IInputOldPinPresenter;
import cn.sirun.com.fca.account.presenter.impl.InputOldPinPresenter;
import cn.sirun.com.fca.account.uitl.AccountStatusUtils;
import cn.sirun.com.fca.account.view.IPinValidateView;

public class PinValidateFlowView extends BaseLoadingFlowView<IInputOldPinPresenter>
        implements IPinValidateView {

    private TextView mNextTV, mForgetTV;
    private GridPasswordView mPinGPV;

    private String mValidatePin;
    private CustomSRPClient mCustomSRPClient;
    private int mFrom;

    public PinValidateFlowView(Activity activity) {
        super(activity);
    }

    public PinValidateFlowView(Activity activity, IContentViewContainer container) {
        super(activity, container);
    }

    @Override
    public void getUpdatePinSetParamsResult(BaseResponse<PinSetParamResponse> response) {
        int code = response.getCode();
        if (code == StatusCode.SUCCESS) {
            /**
             * 获取修改pin参数成功后，进行验证pin
             */

            String salt = response.getData().getSalt();
            String group = response.getData().getGroup();
            String publicKey = response.getData().getB();

            String[] authenticateInfo = SRPUtils.generateAuthenticateInfo(mCustomSRPClient,
                    mValidatePin, salt, group, publicKey);

            VerifyPinRequest request = new VerifyPinRequest(authenticateInfo[0],
                    authenticateInfo[1]);

            getPresenter().verifyPin(request);
        }
    }

    @Override
    public void verifyPinResult(BaseResponse<PinSetParamResponse> response) {
        int code = response.getCode();
        if (code == StatusCode.SUCCESS) {
            if (mFrom == AccountIntentConst.DELETE_CAR_VERIFY_PIN) {
                Intent intent = new Intent();
                getActivity().setResult(AccountIntentConst.DELETE_CAR_VERIFY_PIN_RESULT_CODE, intent);
                getActivity().finish();
            } else if(mFrom == AccountIntentConst.CONTROL_CAR_VERIFY_PIN){
                String path = AccountActivityConst.Path.CONTROL;
                Intent controlIntent = Router.fromPath(path);
                int controlType = getActivity().getIntent()
                        .getIntExtra(ControlIntentConst.CONTROL_TYPE,0);
                controlIntent.putExtra(ControlIntentConst.CONTROL_TYPE, controlType);
                getActivity().startActivity(controlIntent);
                getActivity().finish();
            }
            return;
        }
        DialogUtils.showToast(getContext(), response.getMsg());
    }

    @Override
    protected IInputOldPinPresenter createPresenter() {
        return new InputOldPinPresenter();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mCustomSRPClient = SRPUtils.getCustomSRPClient();
        mFrom = getActivity().getIntent().getIntExtra(AccountIntentConst.FROM, AccountIntentConst.UNKNOWN);
        initView();
    }

    private void initView() {
        setContentView(R.layout.pin_validate_activity);
        final Activity activity = getActivity();
        mPinGPV = activity.findViewById(R.id.gpv_pin);
        mNextTV = activity.findViewById(R.id.tv_pin_next);
        mForgetTV = activity.findViewById(R.id.tv_pin_forget);

        mPinGPV.togglePasswordVisibility();

        mNextTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                handleNextClick();
            }
        });

        mForgetTV.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                Intent intent = Router.fromPath(AccountActivityConst.Path.PERSONAL_CAPTCHA);
                intent.putExtra(AccountIntentConst.FROM, AccountIntentConst.FORGET_ACCOUNT_PIN);
                ActivityUtils.startActivityForResult(activity, intent);
                getActivity().finish();
            }
        });
    }

    private void handleNextClick() {
        mValidatePin = mPinGPV.getPassWord();
        if (!TextUtils.isEmpty(mValidatePin)) {
            PinSetParamsRequest request = new PinSetParamsRequest();
            getPresenter().getUpdatePinSetParams(request);
        }
    }

}
